<?php


namespace App\Http\Controllers\Saas;


use App\Contract\ContractStatus;
use App\Http\Controllers\Controller;
use App\Http\Resources\PaginationCollection;
use App\Model\Agent;
use App\Model\AgentAmountLog;
use App\Model\AgentCashLog;
use App\Model\AgentHasShop;
use App\Model\Contract;
use App\Model\ContractTenant;
use App\Model\TjCountry;
use App\Model\House;
use App\Utils\Result;
use App\Utils\Token;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class TjCountryController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $query = TjCountry::query();
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 10);
        $name = $request->input('name');
        $province = $request->input('province');
        $city = $request->input('city');
        $district = $request->input('district');
        $country = $request->input('country');
        $query->when($province, function($q) use ($province) {
            $q->where('province', $province);
        });
        $query->when($city, function($q) use ($city) {
            $q->where('city', $city);
        });
        $query->when($district, function($q) use ($district) {
            $q->where('district', $district);
        });
        $query->when($country, function($q) use ($country) {
            $q->where('country', $country);
        });
        $query->when($name, function($q) use ($name) {
            $q->where('name', 'like', "%{$name}%");
        });
        $data = $query->latest()->paginate($pageSize, ['*'], 'page', $page);
        return Result::success(new PaginationCollection($data));
    }

    public function show($id)
    {
        $sp = TjCountry::query()->where('id',$id)->first(['id','name', 'province', 'city', 'district','country_code']);
        return Result::success($sp);
    }

    public function add(Request $request)
    {
        $data = $request->only([
            'name',
            'province',
            'city',
            'district',
            'country_code',
            'country'
        ]);

        if(!$data['name'] && !$data['country']){
            return Result::error(401, '请输入镇街道或村社区');
        }
        $sp = new TjCountry();
        $sp->fill($data);
        $sp->save();
        return Result::success();
    }

    public function edit(Request $request, $id)
    {
        $data = $request->only([
            'name',
            'province',
            'city',
            'district',
            'country_code',
            'country'
        ]);
        if(!$data['name'] && !$data['country']){
            return Result::error(401, '请输入镇街道或村社区');
        }
        $sp = TjCountry::find($id);
        $sp->fill($data);
        $sp->save();
        return Result::success();
    }

    /**删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function del($id){
        $sp = TjCountry::find($id);
        $sp->delete();
        return Result::success();
    }

}
